{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.keras import layers,losses,metrics,optimizers\n",
    "\n",
    "#打印时间分割线\n",
    "@tf.function\n",
    "def printbar():\n",
    "    ts = tf.timestamp()\n",
    "    today_ts = ts%(24*60*60)\n",
    "\n",
    "    hour = tf.cast(today_ts//3600+8,tf.int32)%tf.constant(24)\n",
    "    minite = tf.cast((today_ts%3600)//60,tf.int32)\n",
    "    second = tf.cast(tf.floor(today_ts%60),tf.int32)\n",
    "\n",
    "    def timeformat(m):\n",
    "        if tf.strings.length(tf.strings.format(\"{}\",m))==1:\n",
    "            return(tf.strings.format(\"0{}\",m))\n",
    "        else:\n",
    "            return(tf.strings.format(\"{}\",m))\n",
    "\n",
    "    timestring = tf.strings.join([timeformat(hour),timeformat(minite),\n",
    "                timeformat(second)],separator = \":\")\n",
    "    tf.print(\"==========\"*8,end = \"\")\n",
    "    tf.print(timestring)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "n = 800\n",
    "\n",
    "X = tf.random.uniform([n,2],minval=-10,maxval=10)\n",
    "w0 = tf.constant([[2.0],[-1.0]])\n",
    "b0 = tf.constant(3.0)\n",
    "\n",
    "Y = X@w0 + b0 + tf.random.normal([n,1],mean=0.0,stddev=2.0)\n",
    "\n",
    "ds = tf.data.Dataset.from_tensor_slices((X,Y)) \\\n",
    "    .shuffle(buffer_size=1000).batch(100)\\\n",
    "    .prefetch(tf.data.experimental.AUTOTUNE)\n",
    "\n",
    "optimizer = optimizers.SGD(learning_rate=0.001)\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================================================================00:03:38\r\n",
      "epoch = 100 loss = 4.34201288\r\n",
      "w = [[2.00536036]\n",
      " [-1.00451708]]\r\n",
      "b = [2.35160875]\r\n",
      "\r\n",
      "================================================================================00:03:40\r\n",
      "epoch = 200 loss = 3.50318599\r\n",
      "w = [[2.00717974]\n",
      " [-1.0041635]]\r\n",
      "b = [2.8257997]\r\n",
      "\r\n",
      "================================================================================00:03:43\r\n",
      "epoch = 300 loss = 4.47757\r\n",
      "w = [[2.00587082]\n",
      " [-1.00531363]]\r\n",
      "b = [2.92143607]\r\n",
      "\r\n",
      "================================================================================00:03:45\r\n",
      "epoch = 400 loss = 2.60105562\r\n",
      "w = [[2.00576282]\n",
      " [-1.00596452]]\r\n",
      "b = [2.94070935]\r\n",
      "\r\n",
      "================================================================================00:03:48\r\n",
      "epoch = 500 loss = 4.44511127\r\n",
      "w = [[2.00547075]\n",
      " [-1.00622666]]\r\n",
      "b = [2.94456458]\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "linear = layers.Dense(units=1)\n",
    "linear.build(input_shape=(2,))\n",
    "\n",
    "@tf.function\n",
    "def train(epoches):\n",
    "    for epoch in tf.range(1,epoches + 1):\n",
    "        L = tf.constant(0.0)\n",
    "        for X_batch,Y_batch in ds:\n",
    "            with tf.GradientTape() as tape:\n",
    "                Y_hat = linear(X_batch)\n",
    "                loss = losses.mean_squared_error(tf.reshape(Y_hat,[-1]),tf.reshape(Y_batch,[-1]))\n",
    "            grads = tape.gradient(loss,linear.variables)\n",
    "            optimizer.apply_gradients(zip(grads,linear.variables))\n",
    "            L = loss\n",
    "\n",
    "        if (epoch % 100 == 0):\n",
    "            printbar()\n",
    "            tf.print(\"epoch =\",epoch,\"loss =\",L)\n",
    "            tf.print(\"w =\",linear.kernel)\n",
    "            tf.print(\"b =\",linear.bias)\n",
    "            tf.print(\"\")\n",
    "\n",
    "train(500)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}